/* 题目前提条件 */
class Container {
  static of(value) {
    return new Container(value)
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    return Container.of(fn(this._value))
  }
}

class Maybe {
  static of(x) {
    return new Maybe(x)
  }
  isNothing() {
    return this._value === null || this._value === undefined
  }

  constructor(x){
    this._value = x
  }

  map(fn) {
    return this.isNothing() ? this : Maybe.of(fn(this._value))
  }
}
/* 题目前提条件 */

// 练习一
let maybe = Maybe.of([5,6,1])

let ex1 = () => {
  return maybe.map(fp.map(i => {
    return fp.add(i, 1)
  }))
}

// 练习二
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
  return fp.first(xs._value)
}

//练习三
let safeProp = fp.curry(function(x,o) {
  return Maybe.of(o[x])
})

let user = {id: 2, name: 'Albert'}
let ex3 = () => {
  return fp.first(safeProp('name', user)._value)
}

// 练习四
let ex4 = n => {
  return Maybe.of(n).map(parseInt)._value
}